﻿Imports KanBan.Pgd.Services
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Infrastructure.Exceptions

Public Class ManageSearchRequestInterviews

    Private app As New PgdApp()

    Private Const DefaulGridViewRowHeight As Integer = 36

    ''' <summary>
    ''' Property accesor to the shared Search Request.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SearchRequest() As SearchRequest
        Get
            Return EditSearchRequestContainer.SearchRequest
        End Get
        Set(ByVal value As SearchRequest)
            EditSearchRequestContainer.SearchRequest = value
        End Set
    End Property

    Public Sub New(ByVal parentForm As Form)

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.Parent = parentForm

            Me.InitializeView()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub InitializeView()

        Dim interviews = Me.SearchRequest.GetAllInterviews()

        For Each i In interviews
            Me.AddInterview(i)
        Next

    End Sub

    Private Sub AddInterview(ByVal interview As Interview)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvInterviews)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.Id).Value = interview.Id
        row.Cells(GridColumns.DateTime).Value = interview.DateTime
        row.Cells(GridColumns.Candidate).Value = interview.Candidate.Name
        row.Cells(GridColumns.Room).Value = interview.Room.Name

        grvInterviews.Rows.Add(row)

    End Sub

    Private Sub UpdateInterview(ByVal interview As Interview)

        For Each r As DataGridViewRow In grvInterviews.Rows
            If CType(r.Cells(GridColumns.Id).Value, Guid) = interview.Id Then
                r.Cells(GridColumns.DateTime).Value = interview.DateTime
                r.Cells(GridColumns.Candidate).Value = interview.Candidate.Name
                r.Cells(GridColumns.Room).Value = interview.Room.Name
                Exit For
            End If
        Next

    End Sub

    Private Sub AddNewInterview()

        If Me.SearchRequest.Candidates.Count = 0 Then
            UIHelper.WarningConfirmed("You cannot create an Interview because there are no selected Candidates for this Search Request, please select at least one first.")
            Exit Sub
        End If

        Dim form As New EditInterview()
        AddHandler form.InterviewAdded, AddressOf OnInterviewAdded
        form.ShowDialog(Me)

    End Sub

    Private Sub OnInterviewAdded(ByVal interview As Interview)

        Me.AddInterview(interview)

    End Sub

    Private Sub OnInterviewUpdated(ByVal interview As Interview)

        Me.UpdateInterview(interview)

    End Sub

    Private Sub EditInterview(ByVal interviewId As Guid)

        Dim form As New EditInterview(interviewId)
        AddHandler form.InterviewUpdated, AddressOf OnInterviewUpdated
        form.ShowDialog(Me)

    End Sub

    Private Sub btnNewInterview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewInterview.Click
        Me.AddNewInterview()
    End Sub

    Private Sub grvInterviews_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvInterviews.CellClick

        If e.RowIndex >= 0 Then

            Dim interviewId = CType(grvInterviews.Rows(e.RowIndex).Cells(GridColumns.Id).Value, Guid)

            If e.ColumnIndex = GridColumns.Edit Then
                Me.EditInterview(interviewId)
            End If

            If e.ColumnIndex = GridColumns.Delete Then
                If UIHelper.DeletionConfirmed("Are you sure you want to delete this Interview?") Then
                    SearchRequest.RemoveInterview(interviewId)
                    grvInterviews.Rows.Remove(grvInterviews.Rows(e.RowIndex))
                End If
            End If

        End If

    End Sub

    Private Sub grvInterviews_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvInterviews.CellMouseEnter
        If e.RowIndex >= 0 Then
            grvInterviews.Rows(e.RowIndex).Selected = True
            Me.Cursor = Cursors.Hand
        End If
    End Sub

    Private Sub grvInterviews_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvInterviews.CellMouseLeave
        If e.RowIndex >= 0 Then
            grvInterviews.Rows(e.RowIndex).Selected = False
            Me.Cursor = Cursors.Arrow
        End If
    End Sub

    Private Enum GridColumns
        Id
        DateTime
        Candidate
        Room
        Edit
        Delete
    End Enum

End Class